var app = getApp()
var modal = require('../../utils/modal');
var request = require('../../utils/request');
var utils = require('../../utils/utils');
var comment = require('../widget/comment/comment');
var storage = require('../../utils/storage');
var goTop = require('../widget/goTop/goTop');
var timestamp = require('../widget/timestamp');


var pageSetting = {
    callback:function(componentName){
        console.log(componentName)
        var that = this;
        switch(componentName){
            case 'commentContent':
                var data = {
                    'self:community_api:community:comment':{
                        'articleId':that['data']['commentData']['articleid'],
                        'content':arguments[1],
                        'commentType':0,
                        'parentUserId':0,
                        'parentCommentId':0,
                        'pParentCommentId':0,
                        'relateinfo':'',
                        'timestamp': timestamp['timestamp'],
                        'gtk': timestamp['gtk']
                    }
                }
                commentFun(data);
                break;
            case 'likeFun':
                commentLikeFun(arguments[1]);
                break;
            default:
                break;
        }

        //点赞函数
        function commentLikeFun (likeData){
            if(likeData['liketabflag'] != 0){
                return false;
            }
            that.setData({
                likeTabFlag:1
            });
            var data = {
                'self:community_api:community:like':{
                    'articleId':likeData.articleid,
                    'likeType':likeData.type,
                    'timestamp': timestamp['timestamp'],
                    'gtk': timestamp['gtk']
                }
            }
            
            request.post({
                path: '/wxapp_api/api'
            },data)
            .then(function (res){
                if( !!res['self:community_api:community:like']['ret']){
                    modal.toast(res['self:community_api:community:like']['msg']);
                    that.setData({
                        likeTabFlag:0
                    });
                    return false;
                }
                if(likeData.type == 1){
                    that.data['articleData']['my_like'] = 1;
                    that.data['articleData']['like_count'] += 1;
                    that.setData({
                        likeTabFlag:0,
                        articleData:that.data['articleData']
                    });
                }else{
                    that.data['articleData']['my_like'] = 0;
                    that.data['articleData']['like_count'] -= 1;
                    that.setData({
                        likeTabFlag:0,
                        articleData:that.data['articleData']
                    });
                 }
            })
        }

        //评论函数
        function commentFun(commentData){
            request.post({
                path: '/wxapp_api/api'
            },commentData)
            .then(function (res){
                modal.hide();
                if( !!res['self:community_api:community:comment']['ret']){
                    // modal.toast(res['self:community_api:community:comment']['msg']);
                    wx.navigateTo({
                        url: "../error/error"
                    })
                    return false;
                }

                that.data['articleData']['comment_count'] += 1;
                that.setData({
                    articleData:that.data['articleData']
                });
                setTimeout(
                    function(){
                        wx.navigateTo({
                            url: "../comment-list/comment-list?id=" + that['data']['articleData']['article_id'] + "&flag=1"
                        })
                    },1000)
            })
        }
    },
    data: {
        shareContent:{}
    },
    onLoad: function(options) {
        modal.loading();
        var that = this;
        var data = {
            'yp:api:article:detail':{
                'id':options.id
            }
        }

        request.post({
            path: '/wxapp_api/api'
        },data)
        .then(function (res){
            if(res['yp:api:article:detail']['ret'] != 0){
                wx.navigateTo({
                    url: "../error/error"
                })
               // modal.toast(res['yp:api:article:detail']['msg']); 
            }
            var articleDetailData = res['yp:api:article:detail']['data'];
            articleDetailData['article_json_content'] = JSON.parse(articleDetailData['article_json_content']);
            var shareContent = {
                'share_title':articleDetailData['share_title'],
                'share_desc':articleDetailData['share_desc'],
                'shar_url':'pages/article/index?id=' + options['id']
            }
            that.setData({
                articleData:articleDetailData,
                shareContent:shareContent
            });
            modal.hide();
        });
    },
    onShareAppMessage:function(){
        var that = this;
        return {
            title: that['data']['shareContent']['share_title'],
            desc:that['data']['shareContent']['share_desc'],
            path: that['data']['shareContent']['shar_url']
        }
    },
    commentShow:function(e) {
        request.bind().then(() => {
            this.onCommentShow(e)
        })
    }
};

utils.assign(pageSetting,comment,goTop);
Page(pageSetting);